<?php
include("time.php");
//connecting database
$host_name="localhost";
$user_name="root";
$pass_name="theshoppingbuzz";
$db_name="theshoppingbuzz";
@mysql_connect($host_name,$user_name,$pass_name) or die("couldnt connect database");
@mysql_select_db($db_name) or die("no database");
//echo "database connected";

//getting input answer
if(isset($_POST['Submit']))
{
$email=$_POST['email_id'];
$answer=$_POST['sec_a'];
 $query="SELECT email_id,sec_a,password FROM users";
 $result= mysql_query($query) or die(mysql_error());
 $count=mysql_num_rows($result);
 
 //testing purpose;
 //echo "$count";

  while($row = mysql_fetch_array($result))
   {
   if( $row['email_id'] == $email)
   {
	if( $row['sec_a'] == $answer)
	{
	
	//testing code
	//echo $row['email_id'];
	//echo $row['sec_a'];
	//echo $row['password'];
	
	echo "<br />";
	
	function createPassword($length) {
		$chars = "234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$i = 0;
		$password = "";
		while ($i <= $length) {
			$password .= $chars{mt_rand(0,strlen($chars))};
			$i++;
		}
		return $password;
	}
 
$password = createPassword(8);
$ency=sha1($password);
echo "We created new password for you. Your new password is send to your email id $email \n";
echo "To change password in our website click";
?>
<a href="change_password.php">here</a>;

<?php

mysql_query("UPDATE users SET password = '$ency'
WHERE email_id = '$email'") or die("error in inserting");
 



	
		//set php.ini
		//ini_set("SMTP","smtp.gmail.com");
		//ini_set("smtp_port","25");
		//echo ini_get("SMTP");
		//echo ini_get("smtp_port");
		$date=date("d")+2;
		
			// send e-mail to ...
		
		$to="$email"; 

		// Your subject 
		$subject="Your password here"; 

		// From 7
		$header="from: theshoppingbuzz <admin@shoppingbuzz.com>"; 

		// Your message 
		$messages= "Your password for login to our website \r\n";
		$messages.="Your password is  $password\r\n";
		$messages.="This password will expiry at $date . To change password click here";
		  
		

		//send email 
		$sentmail = mail($to,$subject,$messages,$header); 
		
		$exp_date=date("d")+2;
		 timer(date("d"),$exp_date);
	
	}
	}
   }
   }
   else
   {
   
   $curr_date=date("d")+2;
   $exp_date=$curr_date;
   timer($curr_date,$exp_date);
   
   }
 
 
  

	//exec ("/usr/bin/php time.php");
	//popen("./time.php", "w");
 //header('Refresh: 5; url=time.php');

 ?>